package com.fanghaoran.view.manager;

import com.fanghaoran.controller.ManagerController;
import com.fanghaoran.entity.Users;

import javax.swing.*;
import java.awt.*;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * @Author: win11
 * @Date: 2023/03/27/21:51
 * @Description:
 */
public class UserInfoPanel extends JPanel {
    public static final int SEARCH_BY_USERNAME = 0;
    public static final int SEARCH_BY_TELEPHONE = 1;
    public static final int SEARCH_BY_NICKNAME = 2;
    public int searchModel;
    public static JPanel panel;
    private JTable table;

    public UserInfoPanel() throws Exception {
        panel = this;
        this.setLayout(new BorderLayout());
        //搜索栏
        JPanel searchPanel = new JPanel();
        //设置搜索栏位置
        searchPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        //添加搜索栏中组件

        //查询条件的选择列表
        List<String> selectList = new ArrayList<>();
        selectList.add("用户名");
        selectList.add("电话号码");
        selectList.add("昵称");
        Vector<String> vector = new Vector<>(selectList);
        JComboBox<String> selector = new JComboBox<>(vector);
        //设置选择列表显示的行数
        selector.setMaximumRowCount(3);

        //搜索输入框
        JTextField search = new JTextField(30);
        //搜索按钮
        JButton sBtn = new JButton("搜索");
        //拉黑用户
        JButton blockBtn = new JButton("拉黑用户");
        //取消拉黑
        JButton cancelBlockBtn=new JButton("取消拉黑");

        //选择列表条目点击事件
        selector.addItemListener(e -> {
                    if (selector.getSelectedItem() != null) {
                        if (selector.getSelectedItem().equals(selectList.get(0))) {
                            searchModel = SEARCH_BY_USERNAME;
                        } else if (selector.getSelectedItem().equals(selectList.get(1))) {
                            searchModel = SEARCH_BY_TELEPHONE;
                        } else if (selector.getSelectedItem().equals(selectList.get(2))) {
                            searchModel = SEARCH_BY_NICKNAME;
                        }
                    }
                }
        );

        //按钮点击事件
        sBtn.addActionListener(e -> {
                    List<Users> usersList;
                    if (searchModel == SEARCH_BY_USERNAME) {
                        try {
                            //获取指定搜索条件的用户数据
                            usersList = (List<Users>) new ManagerController().getUserInfoByUsername(search.getText()).getMessage();
                            if (usersList != null) {
                                //重新设定表格数据
                                table.setModel(new UserInfoTableModel(usersList));
                            }
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    } else if (searchModel == SEARCH_BY_TELEPHONE) {
                        //获取指定搜索条件的用户数据
                        try {
                            usersList = (List<Users>) new ManagerController().getUserInfoByTelephone(search.getText()).getMessage();
                            if (usersList != null) {
                                //重新设定表格数据
                                table.setModel(new UserInfoTableModel(usersList));
                            }
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    } else if (searchModel == SEARCH_BY_NICKNAME) {
                        //获取指定搜索条件的用户数据
                        try {
                            usersList = (List<Users>) new ManagerController().getUserInfoByNickname(search.getText()).getMessage();
                            if (usersList != null) {
                                //重新设定表格数据
                                table.setModel(new UserInfoTableModel(usersList));
                            }
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                }
        );

        //拉黑用户
        blockBtn.addActionListener(e -> {
                    //获取当前表格选中的条目
                    int selectedRow = table.getSelectedRow();
                    //如果有选择条目,返回行数,没有则返回-1
                    if (selectedRow == -1) {
                        JOptionPane.showMessageDialog(this, "请选择要拉黑的用户条目");
                        return;
                    }
                    //获取当前选中条目的用户名信息
                    String username = (String) table.getValueAt(selectedRow, 1);
                    Long blocked = (Long) table.getValueAt(selectedRow, 8);
                    if (blocked == 0) {
                        try {
                            new BlockFrame(username);
                        } catch (Exception exception) {
                            exception.printStackTrace();
                        }
                    } else {
                        JOptionPane.showMessageDialog(this, "当前用户已被拉黑");
                    }
                }
        );

        //取消拉黑
        cancelBlockBtn.addActionListener(e-> {
            //获取当前表格选中的条目
            int selectedRow = table.getSelectedRow();
            //如果有选择条目,返回行数,没有则返回-1
            if (selectedRow == -1) {
                JOptionPane.showMessageDialog(this, "请选择要取消拉黑的用户条目");
                return;
            }
            //获取当前选中条目的用户名信息
            String username = (String) table.getValueAt(selectedRow, 1);
            Long blocked = (Long) table.getValueAt(selectedRow, 8);
            if (blocked == 1) {
                try {
                    new ManagerController().updateBlocked(0L,username);
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
                JOptionPane.showMessageDialog(this,"取消拉黑成功");
            } else {
                JOptionPane.showMessageDialog(this, "当前用户未被拉黑,无需取消");
            }
            }
        );

        searchPanel.add(selector);
        searchPanel.add(search);
        searchPanel.add(sBtn);
        searchPanel.add(blockBtn);
        searchPanel.add(cancelBlockBtn);
        //放在panel上面位置
        this.add(searchPanel, BorderLayout.NORTH);

        //展示信息的表格
        //数据库获取所有用户信息
        @SuppressWarnings("unchecked")
        List<Users> usersList = (List<Users>) new ManagerController().getUserInfo().getMessage();
        table = new JTable(new UserInfoTableModel(usersList));
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //放在panel中间位置
        this.add(new JScrollPane(table), BorderLayout.CENTER);
    }
}
